package com.atguigu.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.atguigu.commonutils.R;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.utils.ConstansUtils;
import com.atguigu.vod.utils.InitVodClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin
public class VodController {

    @Autowired
    private VodService vodService;
    //上传视频到阿里云
    @PostMapping("uploadAliyunVideo")
    public R uploadAliyunVideo(MultipartFile file){ //得到上传文件 MultipartFile
        //上传视频后 上传方法会返回视频id
        String videoId = vodService.uploadVideoAliyun(file);
        System.out.println("视频ID==========================="+videoId);
        return R.ok().data("videoId",videoId);
    }
    //根据视频id删除视频
    @DeleteMapping("removeAliyunVideo/{id}")
    public R removeVideoById(@PathVariable String id){
        try {
            //获取自定义对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstansUtils.ACCESS_KEY_ID, ConstansUtils.ACCESS_KEY_SECRET);
            //创建删除Video的request对象
            DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest();
            deleteVideoRequest.setVideoIds(id);
            //调用client对象实现删除
            client.getAcsResponse(deleteVideoRequest);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw  new GuliException(20001,"删除视频失败");
        }
    }
    //删除多个阿里云视频
    @DeleteMapping("deleteBatch")
    public R deleteBatch(@RequestParam("videoIdList") List<String> videoIdList){
        vodService.removeMoreAlyVideo(videoIdList);
        return  R.ok();
    }


}
